default_arch:=arch()
default_target:=shell("cjc -v|tail -n 1|awk '{print $2}'")
default_os:=os()
default: macos
    echo '{{GREEN}}success{{NORMAL}}'
_download_lua54:
    #!/usr/bin/env bash
    echo 'check lua54 if exists'
    file_name="lua54.tar.gz"
    if [[ ! -d "lua54" ]]; then
       curl -L -C - -o "$file_name" https://lua.org/ftp/lua-5.4.8.tar.gz
       tar -xzvf "$file_name" 
       mv 'lua-5.4.8' lua54/
       rm "$file_name"
       echo 'prepare lua54 {{GREEN}}success{{NORMAL}}'
    else
        echo 'lua54 {{GREEN}}exists{{NORMAL}}'
    fi
macos arch=default_arch:
    #!/usr/bin/env bash
    # x86_64-w64-mingw32
    # aarch64-apple-darwin
    # x86_64-apple-darwin
    target=''
    case "{{arch}}" in
    x86_64)
    target="x86_64-apple-darwin"
    ;;
    aarch64)
    target="aarch64-apple-darwin"
    ;;
    *)
    echo "{{RED}}unsupport arch: {{arch}}{{NORMAL}}"
    exit 1
    ;;
    esac
    just lua macos
    just build "$target"
linux arch=default_arch:
    #!/usr/bin/env bash    
    # x86_64-unknown-linux-gnu
    # aarch64-unknown-linux-gnu
    target=''
    case "{{arch}}" in
    x86_64)
    target="x86_64-unknown-linux-gnu"
    ;;
    aarch64)
    target="aarch64-unknown-linux-gnu"
    ;;
    *)
    echo "{{RED}}unsupport arch: {{arch}}{{NORMAL}}"
    exit 1
    ;;
    esac
    just lua linux
    just build "$target"
windows arch=default_arch:
    #!/usr/bin/env bash    
    # x86_64-unknown-linux-gnu
    # aarch64-unknown-linux-gnu
    target=''
    case "{{arch}}" in
    x86_64)
    target="x86_64-w64-mingw32"
    ;;
    *)
    echo "{{RED}}unsupport arch: {{arch}}{{NORMAL}}"
    exit 1
    ;;
    esac
    just lua windows
    just build "$target"
build target="":
    @if [[ "{{target}}" == "{{default_target}}" ]]; then \
    cjpm build; \
    else \
    cjpm build --target {{target}}; \
    fi
lua plat=default_os: _download_lua54
    #!/usr/bin/env bash
    platform='{{plat}}'
    case "{{plat}}" in
    macos)
    platform="macosx"
    ;;
    linux)
    ;;
    windows)
    platform='mingw'
    ;;
    *)
    echo "{{RED}}unsupport os: {{plat}}{{NORMAL}}"
    exit 1
    ;;
    esac
    cd lua54 && make "$platform"
fmt:
    cjfmt -d src
    cjfmt -d examples
clean:
    cjpm clean
    cd lua54 && make clean